export class Ws {
    webSocket = null
    handler = null
    constructor() {
        this.webSocket = new WebSocket('ws:localhost:3009')
        const _webSocket = this.webSocket

        _webSocket.addEventListener('open', (e) => {
            console.log('webSocket open', e);
        })
        _webSocket.addEventListener('close', () => {
            console.log('webSocket close');
        })
        _webSocket.addEventListener('error', () => {
            console.log('webSocket error');
        })
        _webSocket.addEventListener('message', (e) => {
            if (this.handler) {
                this.handler(JSON.parse(e.data))
            }
        })
    }

    send(msg) {
        this.webSocket.send(msg)
    }

    on(cb) {
        this.handler = cb
    }
}
